<?php
/**
 * 《车辆信息》实现类
 *
 * @author 孙晓晔
 * @version $Id: Car.class.php 1033 2010-03-25 11:26:55Z sunxy $
 */
require_once(dirname(__FILE__) . '/Machine.class.php');

class Car extends Machine {
	
	public $type = '1';
	
	public $machine1 = array(
		'-' => '请选择',
		'1' => '单位',
		'2' => '个人',

		'9' => '其它',
	);
	
	public $machine2 = array(
		'-' => '请选择',
		'1' => '轿车',
		'2' => '商务车',
		'3' => '货车',

		'9' => '其它',
	);	

	public $orderby = array(
		'-' => '请选择',
		'emp' => '司机',
		'date' => '日期',
		
		'1' => '创建时间',
		'2' => '更新时间',
	);

	function __construct() {
		$request['update'] = '$Date: 2009/02/16 07:36:07 $';
		$request['revision'] = '$Revision: 1.1 $';

		parent::__construct($request);
	}

	function pretreat($controller) {
		parent::pretreat($controller);
		
		$controller->assign_by_ref('machine1_option', $this->machine1);
		$controller->assign_by_ref('machine2_option', $this->machine2);
	}

	function tab($controller, $request) {		
		$data = array(
			'用车需求' => array('cc'=>'car.php?do=Requirement.find', 'js'=>'1'),
			'用车安排' => array('cc'=>'car.php?do=Plan.find', 'js'=>'1'),
			'加油记录' => array('cc'=>'car.php?do=Oil.find', 'js'=>''),
			
			'车辆信息' => array('cc'=>'car.php?do=Car.find', 'js'=>'1'),
			'维修单' => array('cc'=>'car.php?do=Maintain.find', 'js'=>''),
				
			'统计分析' => array('cc'=>'car.php?do=Car.tj_tab', 'js'=>''),
		);
		
		$ret = $this->_tab($data);

		$controller->assign_by_ref('result', $ret);

		$controller->display($request, 'tab');
		return true;
	}
	
	function tj_tab($controller, $request) {		
		$data = array(
			'用车需求' => array('cc'=>'car.php?do=Requirement.group', 'js'=>''),
			'用车安排' => array('cc'=>'car.php?do=Plan.group', 'js'=>''),
			'车辆服务' => array('cc'=>'car.php?do=Maintain.group', 'js'=>''),			
		);
		
		$ret = $this->_tab($data);

		$controller->assign_by_ref('result', $ret);

		$controller->display($request, 'tj_tab');
		return true;
	}
	
	function mobile($controller, $request) {
		$this->pretreat($controller);
		
		global $CategoryTree;
		
		$data = $CategoryTree['行政中心'];
		
		$ret = array();
		foreach ($data as $k => $v) {
			if ($this->tree_my($v)) {
				$ret[$k] = $v;
			}
		}
		
		$controller->assign('menu', $ret);
	
		$controller->display($request, 'mobile');
		return true;
	}
	
	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function find($controller, $request) {
		$this->pretreat($controller);

		$controller->display($request, 'car/machine/car/find');
		return true;
	}

	function _orderby_x($controller, $row, $px) {
		$orderby = $this->orderby['1'];
		$row['orderby_x'] = $row['createtime'];

		if (empty($px) || $px == '-' || $px == '1') {
			$controller->assign('orderby', $orderby);

			return $row;
		}

		if ($px == '2') {
			$orderby = $this->orderby[$px];
			$row['orderby_x'] = $row['modifytime'];
		}

		if ($px == 'mobile') {
			$orderby = $this->orderby[$px];
			$row['orderby_x'] = $row['mobile'];
		}

		$controller->assign('orderby', $orderby);

		return $row;
	}
	
	function _my() {
		if ($this->_is_admin()) {
			return '';
		}

		return "visible = 1";
	}

	function _where($request) {
		
		$name_s = $request['name_s'];
		
		$machine1_s = $request['machine1_s'];
		$machine2_s = $request['machine2_s'];

		$my = $this->_my();

		if (empty($my)) {
			$where = " WHERE 1=1";
		} else {
			$where = " WHERE " . $my;
		}
		
		$where .= " AND type = 1";
		
		if (!empty($name_s)) {
			$where .= " AND (name like '%" . $name_s . "%' OR content like '%" . $name_s . "%' OR json like '%" . $name_s . "%')";
		}
		
		if (!empty($machine1_s) && $machine1_s <> '-') {
			$where .= " AND machine1 = '" . $machine1_s . "'";
		}
		
		if (!empty($machine2_s) && $machine2_s <> '-') {
			$where .= " AND machine2 = '" . $machine2_s . "'";
		}
		
		$where .= $this->_where_default($request);
		$where = $this->_where_aa($where);

		return $where;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function findPost($controller, $request) {
		$this->pretreat($controller);

		$this->findPara($controller, $request);

		$db = newdb($this->dsn_r);
		$fmt = "SELECT * FROM %s a %s %s";
		$where = $this->_where($request);
		$orderby = $this->_orderby();
		$sql = sprintf($fmt, $this->tbl, $where, $orderby);
		debug($sql);

		$pager_option = array (
            'db' => $db,
            'sql' => $sql,
            'PageSize' => $this->pagesize,
            'CurrentPageID' => $request['p'],
            'numItems' => $request['n']
		);

		$pager = @new Pager($pager_option);
		$data = $pager->getPageData();

		if ($pager->number > 0) {

			$count = 0;
			$from = $pager->from;
			while ($count < $pager->number) {
				$row = $data[$count];
				$row = $this->_view(null, $row);

				$row = $this->_orderby_x($controller, $row, $this->orderby_s);

				$data[$count] = $row;
				$count++;
			}
			$controller->assign_by_ref('result', $data);
			$controller->assign_by_ref('pager', $pager);
		}

		$controller->display($request, 'car/machine/car/list');
		return true;
	}
	
	function xls($controller, $request) {
		$this->pretreat($controller);
	
		$this->findPara($controller, $request);
	
		$request['tpl'] = "xls";
	
		$db = newdb($this->dsn_r);
		$fmt = "SELECT * FROM %s a %s %s";
		$where = $this->_where($request);
		$orderby = $this->_orderby();
		$sql = sprintf($fmt, $this->tbl, $where, $orderby);
		debug($sql);
	
		$result = $db->query($sql);
		$data = array();
		while ($row = $db->fetch_array($result)) {
			$row = $this->_view(null, $row);
	
			$row = $this->_orderby_x($controller, $row, $this->orderby_s);
	
			$data[] = $row;
		}
	
		$controller->assign_by_ref('result', $data);
	
		$filename = sprintf("%s.xls", date("YmdHis"));
		header('Content-Disposition:application/vnd.ms-excel; filename=' . $filename);
	
		if ($request['xls'] == '2') {
			$controller->display($request, 'car/machine/car/list_2');
		} else {
			$controller->display($request, 'car/machine/car/list');
		}
	
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function my($controller, $request) {

		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function add($controller, $request) {
		$this->pretreat($controller);

		$controller->assign('t1', strtotime('-3 day'));

		$controller->display($request, 'car/machine/car/add');
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function addPost($controller, $request) {
		$this->pretreat($controller);

		$emp = $this->_input($request['emp']);
		$office = $this->_office($emp);
		$dept = $this->_emp_dept($emp);
		
		$date = $request['date'];
		
		$code = $request['code'];
		$name = $request['name'];
				
		$zjm = getPinyin($name);
		$zjm = strtoupper($zjm);

		$supplier = $this->_input($request['supplier']);
		$supplier_x = addslashes($request['supplier_x']);
		
		$content = addslashes($request['content']);
		
		$machine1 = $request['machine1'];
		$machine2 = $request['machine2'];
		
		$type = '1';
		$ext_x = addslashes($request['ext_x']);

		$db = newdb($this->dsn_w);
		$fmt = "INSERT INTO %s(office, dept, emp, date, supplier, supplier_x, code, name, zjm, content, type, json, oper, createtime, ";
		$fmt .= "modifytime) VALUES('%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', now(), now())";
		$sql = sprintf($fmt, $this->tbl, $office, $dept, $emp, $date, $supplier, $supplier_x, $code, $name, $zjm, $content, $machine1, $machine2, $type, $ext_x, $_SESSION['id']);
		// debug($sql);
		$db->query($sql);
		
		$request['id_s'] = $db->insert_id();

		$controller->assign('baseLink', $this->doGet($request, 'findPost'));

		$this->findPost($controller, $request);
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function edit($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];
		$row = $this->_view($id);

		$controller->assign_by_ref('result', $row);

		$controller->display($request, 'car/machine/car/edit');
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function editPost($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];
		clean($this->tbl . "_id_" . $id);

		$emp = $this->_input($request['emp']);
		$office = $this->_office($emp);
		$dept = $this->_emp_dept($emp);
		
		$date = $request['date'];
		
		$code = $request['code'];
		$name = $request['name'];
				
		$zjm = getPinyin($name);
		$zjm = strtoupper($zjm);

		$supplier = $this->_input($request['supplier']);
		$supplier_x = addslashes($request['supplier_x']);
		
		$content = addslashes($request['content']);
		
		$machine1 = $request['machine1'];
		$machine2 = $request['machine2'];
		
		$type = '1';
		$ext_x = addslashes($request['ext_x']);

		$db = newdb($this->dsn_w);
		$fmt = "UPDATE %s SET office = '%s', dept = '%s', emp = '%s', date = '%s', supplier = '%s', supplier_x = '%s', code = '%s', name = '%s', zjm = '%s'";
		$fmt .= ", content = '%s', machine1 = '%s', machine2 = '%s', json = '%s', modifytime = now() WHERE id = '%s'";
		$sql = sprintf($fmt, $this->tbl, $office, $dept, $emp, $date, $supplier, $supplier_x, $code, $name, $zjm, $content, $machine1, $machine2, $ext_x, $id);
		// debug($sql);
		$db->query($sql);
		if ($db->error()) {
			$controller->assign("msg", "修改失败，请联系管理员" . $db->error());
			$controller->display($request, 'info');
			return false;
		}

		$controller->assign('baseLink', $this->doGet($request, 'findPost'));

		$this->findPost($controller, $request);
		return true;
	}
	
	function copy($controller, $request) {
		$this->pretreat($controller);
	
		$id = $request['id'];
		$row = $this->_view($id);
	
		$controller->assign_by_ref('result', $row);
	
		$controller->display($request, 'car/machine/car/copy');
		return true;
	}
	
	function upload($controller, $request) {
		$this->pretreat($controller);
	
		$id = $request['id'];
		
		$this->_files_clean($id);
		
		$row = $this->_view($id);
	
		$controller->assign_by_ref('result', $row);
	
		$controller->display($request, 'car/machine/car/upload');
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function view($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];
		$row = $this->_view($id, null, false);

		$controller->assign_by_ref('result', $row);

		$controller->display($request, 'car/machine/car/view');
		return true;
	}

	function _view($id = 0, $row = null) {
		
		$row = parent::_view($id, $row);
		
		$row['type_t'] = $this->type[$row['type']];
		
		return $row;
	}
	
	function delete($controller, $request) {
		$this->pretreat($controller);

		$db = newdb($this->dsn_w);
		while (list($k, $id) = each($request)) {
			if (substr($k, 0, 2) <> "c_") {
				continue;
			}

			$fmt = "UPDATE %s SET visible = 2 WHERE id = '%s'";
			$sql = sprintf($fmt, $this->tbl, $id);
			debug($sql);
			$db->query($sql);
		}

		$controller->assign('baseLink', $this->doGet($request, 'findPost'));

		$this->findPost($controller, $request);
		return true;
	}
	
	function z($controller, $request) {
		$this->pretreat($controller);
	
		$db = newdb($this->dsn_w);
		$fmt = "SELECT * FROM %s";
		$sql = sprintf($fmt, $this->tbl);
		$result = $db->query($sql);
		while ($row = $db->fetch_array($result)) {
			$json = $this->json($row);
	
			$fmt = "UPDATE %s SET json = '%s' WHERE id = '%s'";
			$sql = sprintf($fmt, $this->tbl, $json, $row['id']);
			echo $sql . "\n";
			$db->query($sql);
		}
	
		logdate($controller, $request);
		return true;
	}
	
	function json($data) {
		
		$a = array();
	
		$a['type'] = urlencode($data['type']);
		$a['category'] = urlencode($data['category']);
		
		$a['cpxh'] = urlencode($data['cpxh']);
		$a['nsr'] = urlencode($data['nsr']);
		$a['fdjh'] = urlencode($data['fdjh']);
		$a['cjh'] = urlencode($data['cjh']);
		$a['buydate'] = urlencode($data['buydate']);
		$a['name4s'] = urlencode($data['name4s']);
		
		$a['address4s'] = urlencode($data['address4s']);
		$a['park'] = urlencode($data['park']);
		
		return urldecode(json_encode($a));
		
	}
	
	function t($controller, $request) {
		
		
		echo "\n" . $_REQUEST['do'] . "\n";

		logdate($controller, $request);
		return true;
	}
}
?>
